<?php
class importclass {

	function importclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}

	function reset(){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `session_id`='".session_id()."'", __LINE__, __FILE__);
	}
	
	function get_import_source(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `source` FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `session_id`='".session_id()."' ORDER BY `source`", __LINE__, __FILE__);		
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$row['source']] = "x";
		}
		return $result;
	}
	
	function get_import_header($source=0){
		$result = array();
		if ($source==0){
			$sql = $this->mysqlclass->mysql_my_query("SELECT MIN(`source`), MIN(`row`) FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `session_id`='".session_id()."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$row = mysql_fetch_array($sql);
				if (($row[0] != '') && ($row[1] != 0)){
					$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `source`='".$row[0]."' AND `row`=".$row[1]." AND `session_id`='".session_id()."' ORDER BY `col`", __LINE__, __FILE__);
					while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
						$row['value'] = stripslashes($row['value']);
						$result[] = $row;
					}
				}
			}
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT MIN(`row`) FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `source`='".$source."' AND `session_id`='".session_id()."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$row = mysql_fetch_array($sql);
				if (($row[0] != '')){
					$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `source`='".$source."' AND `row`=".$row[0]." AND `session_id`='".session_id()."' ORDER BY `col`", __LINE__, __FILE__);
					while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
						$row['value'] = stripslashes($row['value']);
						$result[] = $row;
					}
				}
			}
		}
		return $result;
	}

	function import_products_to_shop($parent, $arrays){
		$return_result = array();
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `source`='".$arrays['main']['source']."' AND `session_id`='".session_id()."' ORDER BY `row` ASC", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `session_id`='".session_id()."' AND `id`=".$row['id'], __LINE__, __FILE__);
		}

		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `source`='".$arrays['main']['source']."' AND `session_id`='".session_id()."' ORDER BY `row`, `col`", __LINE__, __FILE__);		
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$row['row']][$row['col']] = $row['value'];
		}
		
		/*
		foreach($result as $key=>$el){
			unset($result[$key]);
			break;
		}
		*/
		
		if (!isset($result) || (count($result) == 0)) return $return_result;
		
			foreach($result as $el_key=>$el){
				$insert = true;
				if ((isset($arrays['rewrite'])) && (count($arrays['rewrite']) > 0)){
					foreach($arrays['rewrite'] as $lng=>$ar){
						//if (!($insert)) break;
						foreach($ar as $key=>$rewrite){
							if (isset($el[$key])) {
								if ($rewrite['type'] == "group"){
									$sql = $this->mysqlclass->mysql_my_query("SELECT `parent` FROM `".$this->setupdata['table_prefix']."product_data` WHERE '".addslashes($el[$key])."' = `value` AND `name`='".$rewrite['name']."' AND `lang`='".$lng."'", __LINE__, __FILE__);						
									if (mysql_num_rows($sql) > 0){
										while (($row = mysql_fetch_array($sql, MYSQL_ASSOC)) && ($insert)) {
											$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$row['parent']." = `id`", __LINE__, __FILE__);						
											if (mysql_num_rows($sql2) > 0){
												$row2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
												if ($row2['parent'] == $arrays['main']['group']) {
													if ($arrays['main']['dublicate'] == 'skip') {
														$return_result['skip'][$el_key] = $el;
														$insert = false;
													}
													elseif ($arrays['main']['dublicate'] == 'overide'){
														$return_result['overide'][$el_key] = $el;
														$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$row['parent']." = `parent`", __LINE__, __FILE__);		
														$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$row['parent']." = `id`", __LINE__, __FILE__);		
													}
												}
											}
										}
									}
								} elseif ($rewrite['type'] == "global"){
									$sql = $this->mysqlclass->mysql_my_query("SELECT `parent` FROM `".$this->setupdata['table_prefix']."product_data` WHERE '".addslashes($el[$key])."' = `value` AND `name`='".$rewrite['name']."' AND `lang`='".$lng."'", __LINE__, __FILE__);
									if (mysql_num_rows($sql) > 0){
										if ($arrays['main']['dublicate'] == 'skip'){
											$return_result['skip'][$el_key] = $el;
											$insert = false;
											break;
										} elseif ($arrays['main']['dublicate'] == 'overide'){
											$row = mysql_fetch_array($sql, MYSQL_ASSOC);
											$return_result['overide'][$el_key] = $el;
											$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$row['parent']." = `parent`", __LINE__, __FILE__);		
											$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$row['parent']." = `id`", __LINE__, __FILE__);		
										}
									}
								}
							}
						}
					}
				}
				if ($insert) {
					if ((isset($el[$arrays['main']['name']])) && (strlen($el[$arrays['main']['name']]) > 0)){
						$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$parent." = `parent`", __LINE__, __FILE__);
						if (mysql_num_rows($sql) < 1) $pos = 0;
						else {
							$row = mysql_fetch_array($sql);
							$pos = $row[0] + 1;
						}
						$el[$arrays['main']['name']] = addslashes($el[$arrays['main']['name']]);
						$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `parent`!=0 AND ".$arrays['main']['group']."=`id`", __LINE__, __FILE__);
						if (mysql_num_rows($sql) == 1){
							$group_info = mysql_fetch_array($sql, MYSQL_ASSOC);
							$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_catalog` (`id`, `name`, `hide`, `pos`, `parent`, `type`, `time_create`, `time_modif`, `author`, `editor`) VALUES ('', '".$el[$arrays['main']['name']]."', 0, ".$pos.", ".$parent.", '".$group_info['type']."', '".date("YmdHis")."', '".date("YmdHis")."', '".$_SESSION['login_user']."', '".$_SESSION['login_user']."')", __LINE__, __FILE__);
							$product_id = mysql_insert_id();
							if ((isset($arrays['cols'])) && (count($arrays['cols'])>0))
							foreach($arrays['cols'] as $lng=>$ar){
								$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_discribe` WHERE `type`='".$group_info['type']."' AND (`lang`='' OR `lang`='".$lng."')", __LINE__, __FILE__);
								while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
									$product_types[$row['name']]['type_of_value'] = $row['type_of_value'];
									$product_types[$row['name']]['pos'] = $row['pos'];
								}
								foreach($ar as $key=>$elem){
									if ((isset($el[$key])) && (strlen($el[$key]) > 0)){
										$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$product_id." = `parent`", __LINE__, __FILE__);
										if (mysql_num_rows($sql) < 1) $pos = 0;
										else {
											$row = mysql_fetch_array($sql);
											$pos = $row[0] + 1;
										}
										$el[$key] = addslashes($el[$key]);
										if ($product_types[$elem]['type_of_value'] == 'chekbox'){
											if (strlen($el[$key]) > 0) $el[$key] = 'on';
											$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_data` (`id`, `parent`, `name`, `value`, `type`, `pos`, `lang`) VALUES ('', ".$product_id.", '".$elem."', '".$el[$key]."', '".$product_types[$elem]['type_of_value']."', '".$product_types[$elem]['pos']."', '".$lng."')", __LINE__, __FILE__);
										} elseif ($product_types[$elem]['type_of_value'] == 'price'){
											$el[$key] = intval($el[$key]);
											$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_data` (`id`, `parent`, `name`, `value`, `type`, `pos`, `lang`) VALUES ('', ".$product_id.", '".$elem."', '".$el[$key]."', '".$product_types[$elem]['type_of_value']."', '".$product_types[$elem]['pos']."', '".$lng."')", __LINE__, __FILE__);
										} else {
											$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_data` (`id`, `parent`, `name`, `value`, `type`, `pos`, `lang`) VALUES ('', ".$product_id.", '".$elem."', '".$el[$key]."', '".$product_types[$elem]['type_of_value']."', '".$product_types[$elem]['pos']."', '".$lng."')", __LINE__, __FILE__);
										}
									}
								}
							}
						}
						$return_result['ok'][$el_key] = $el;
					}
				}
			}
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."import_temp` WHERE `session_id`='".session_id()."'", __LINE__, __FILE__);
		$return_result2 = array();
		foreach($return_result['ok'] as $elem){
			if (
				(!(isset($return_result['overide']))) || (!in_array($elem, $return_result['overide']))
				) $return_result2[] = $elem;
		}
		$return_result['ok'] = $return_result2;
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`, `name`, `value`) VALUES ('', 'import_".session_id()."', '".addslashes(serialize($return_result))."')", __LINE__, __FILE__);		
	}	
	
}
?>
